package org.skydiveapp;

import org.skydiveapplibrary.repository.LogEntryRepository;
import org.skydiveapplibrary.ui.LogEntryListCursorAdapter;
import org.skydiveapplibrary.ui.NumberDialog;
import org.skydiveapplibrary.ui.NumberDialog.OnNumberSetListener;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

public class LogEntryListActivity extends BaseListActivity
{
	// request codes
	private static final int REQUEST_SELECT_LOG_ENTRIES = 0;
	
	// repository
	private LogEntryRepository repository = new LogEntryRepository(this);
	private Cursor cursor;
	
	// menus
	private MenuItem addMenu;
	private MenuItem gotoMenu;
	private MenuItem signMenu;
	private MenuItem copyMenu;
	
	// goto number listener
	private OnNumberSetListener goToSetListener = new OnNumberSetListener()
	{
		@Override
		public void onNumberSet(NumberDialog dialog, int number)
		{
			int position = 0;
			try
			{
				cursor.moveToFirst();
				while (cursor.moveToNext())
				{
					if (cursor.getInt(1) == number)
					{
						position = cursor.getPosition();
					}
				}
			} 
			finally
			{
			
			}

			getListView().setSelection(position);
		}
	};
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
				
		getListView().setChoiceMode(ListView.CHOICE_MODE_NONE);
    	setHeaderText(R.string.log_book_label);
    	
    	// set adapter
    	cursor = repository.getLogEntryCursor();
    	startManagingCursor(cursor);
		setListAdapter(new LogEntryListCursorAdapter(this, cursor));
	}
	
    @Override
    protected void onListItemClick(ListView lv, View view, int position, long id)
    {
		Intent intent = new Intent(this, LogEntryEditActivity.class);
		intent.putExtra(Constants.EXTRA_LOG_ENTRY_ID, (int)id);
		startActivity(intent);
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		addMenu = menu.add(R.string.add_button);
		addMenu.setIcon(R.drawable.ic_menu_add);
		
		copyMenu = menu.add(R.string.log_entry_copy_label);
		copyMenu.setIcon(R.drawable.ic_menu_copy);
		
		signMenu = menu.add(R.string.log_entry_list_sign_button);
		signMenu.setIcon(R.drawable.ic_menu_sign);
		
		gotoMenu = menu.add(R.string.log_entry_list_goto_button);
		gotoMenu.setIcon(R.drawable.ic_menu_search);
		
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		if (item == addMenu)
		{
			Intent intent = new Intent(this, LogEntryEditActivity.class);
        	startActivity(intent);
        	return true;
		}
		else if (item == gotoMenu)
		{
			// init dialog
    		NumberDialog dialog = new NumberDialog(
    				this,
    				R.string.log_entry_list_goto_title,
    				1,
    				goToSetListener);
    		// show dialog
    		dialog.show();
    		return true;
		}
		else if (item == signMenu)
		{
			signLogEntries();
        	return true;
		}
		else if (item == copyMenu)
		{
			Intent intent = new Intent(this, LogEntryEditActivity.class);
			intent.putExtra(Constants.EXTRA_LOG_ENTRY_COPY, (boolean)false);
			startActivity(intent);
			return true;
		}
		else
		{
			return super.onOptionsItemSelected(item);
		}
	}
	
	private void signLogEntries()
	{
    	Intent intent = new Intent(this, LogEntrySelectListActivity.class);
    	startActivityForResult(intent, REQUEST_SELECT_LOG_ENTRIES);
	}
}